package com.etc.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl;

/**
 * 数据库通用工具
 *方法:1.连接 2.增删改查 3.释放资源
 */
public class DBUtil {
	private static final String URL = "jdbc:mysql://localhost:3306/db_bes";//主机号/端口号/数据库名
	private static final String USER = "root";
	private static final String PASSWORD = "root";

	/**
	 * 连接数据库,返回连接对象
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	public static Connection getConn() throws SQLException, ClassNotFoundException {
		//加载驱动,高版本可省略
		Class.forName("com.mysql.jdbc.Driver");
		//返回连接对象
		return DriverManager.getConnection(URL, USER, PASSWORD);
	} 
	/**
	 * 通用对数据库进行增删改操作
	 * @param sql 需要执行的sql语句
	 * @param 参数
	 * @return true更新成功 false更新失败
	 */
	public static boolean executeUpdate(String sql,Object...parms) {
		Connection conn=null;
		PreparedStatement pstm=null;
		int n=0;
		try {
			//创建连接对象
			conn=getConn();
			//得到parparedStatement对象
			pstm=conn.prepareStatement(sql);
			//给占位符?赋值  调用方法
			setPreparedStatement(pstm,parms);
			//执行sql语句
			n=pstm.executeUpdate();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//释放资源
			closeAll(null,pstm,conn);
		}
		return n>0;
	}
	/**
	 * 补齐占位符?的值
	 */
	public static void setPreparedStatement(PreparedStatement pstm,Object...parms) {
		if (null!=pstm && null!=parms) {
			for (int i = 0; i < parms.length; i++) {
				try {
					pstm.setObject(i+1, parms[i]);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} 
	}
	/**
	 * 修改
	 */
	public static CachedRowSet execQuery(String sql,Object... params) {
		//获取连接对象
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		CachedRowSet crs=null;
		try {
			//创建缓存结果集的对象
			crs=new CachedRowSetImpl();
			conn=getConn();
			//通过连接对象得到PreparedStatement对象
			pstm=conn.prepareStatement(sql);
			//给sql中的?赋值
			setPreparedStatement(pstm,params);//这个方法的实现在上面
			//要执行sql
			rs=pstm.executeQuery();
			//ResultSet与CachedRowSet之间怎么转换
			crs.populate(rs);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//释放资源
			closeAll(rs, pstm, conn);
		}
		return crs;
	}
	/**
	 * 释放资源
	 */
	public static void closeAll(ResultSet rs,PreparedStatement pstm,Connection conn) {//先开后关
		if (null!=rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 
		if (null!=pstm) {
			try {
				pstm.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (null!=conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}
